﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TroTro
{
    class Pothole : Sprite
    {
        #region Fields

        private readonly Game _game;
        public float Speed { get; set; }
        public Rectangle Bounds;

        private Vector2 _screenBounds;
        public Pothole(Game game, Vector2 roadBounds)
            : base(game)
        {
            _screenBounds = roadBounds;
            _game = game;
            LoadContent();
        }

        #endregion

        #region Methods

        public override void Update(GameTime gameTime)
        {
            Position.Y += Speed;
            base.Update(gameTime);
        }

        protected override sealed void LoadContent()
        {
            var r = new Random();
            var image = r.Next(4);
            Texture = _game.Content.Load<Texture2D>(@"sprites\pothole" + image);
            //Calculate bounds based on pothole chosen
            switch (image)
            {
                case 0:
                    Bounds = new Rectangle(15, 20, 100, 80);
                    break;
                case 1:
                    Bounds = new Rectangle(10, 10, 60, 60);
                    break;
                case 2:
                    Bounds = new Rectangle(15, 30, 135, 105);
                    break;
                case 3:
                    Bounds = new Rectangle(20, 80, 220, 120);
                    break;
            }


            Position.Y = -100;
            var pos = r.Next(Convert.ToInt32((_screenBounds.Y - Texture.Width / 2.0F))) + (_screenBounds.X + Texture.Width / 2.0F);
            Position.X = pos;

            base.LoadContent();
        }

        #endregion

        #region Bounds

        public Rectangle GetBounds()
        {
            //If center
            return new Rectangle((int)Position.X - (Texture.Width / 2) + Bounds.X,
                                 (int)Position.Y - (Texture.Height / 2) + Bounds.Y,
                                 Bounds.Width,
                                 Bounds.Height);
            //If top-left
            //return new Rectangle((int)Position.X + Bounds.X,
            //                     (int)Position.Y + Bounds.Y,
            //                     Bounds.Width,
            //                     Bounds.Height);
        }

        #endregion
    }
}
